package com.yanqu.road.dao.impl.player;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.player.UserVehicleDetail;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.*;

public class UserVehicleDetailDaoImpl extends TempDataObjectDao<UserVehicleDetail> {

	public UserVehicleDetail getUserVehicleDetail(long userId) {
		String sql = "select * from t_u_vehicle_detail where userId=?";
		ResultSet rs = null;
		CallableStatement call = null;
		try {
			getDb().setConnName(DbNameStrategy.getUserDb());
			call = getDb().getConnection().prepareCall(sql);
			call.setLong(1, userId);
			rs = call.executeQuery();
			while (rs.next()) {
				UserVehicleDetail data = parse(rs);
				return data;
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			getDb().closeConnection(call, rs);
		}
		return null;
	}

	@Override
	protected boolean addData(UserVehicleDetail data) {
		PreparedStatement call = null;
		String sql = "INSERT INTO t_u_vehicle_detail(userId,templateIdList,modifyTime,skillModifyTotalTimes,totalCount,totalDeleteCount,"
				+ "qualitySixAboveTotalCount,totalUpgradeTimes,lastResetTime,enableAutoRecycle,autoRecycleList) VALUES(?,?,?,?,?,?,?,?,?,?,?)";
		try {
			getDb().setConnName(DbNameStrategy.getUserDb());
			call = getDb().getConnection().prepareStatement(sql);
			int i = 1;
			call.setLong(i++, data.getUserId());
			call.setString(i++, StringUtils.listToString(data.getTemplateIdList(), ','));
			call.setLong(i++, data.getModifyTime());
			call.setLong(i++, data.getSkillModifyTotalTimes());
			call.setLong(i++, data.getTotalCount());
			call.setLong(i++, data.getTotalDeleteCount());
			call.setLong(i++, data.getQualitySixAboveTotalCount());
			call.setLong(i++, data.getTotalUpgradeTimes());
			call.setLong(i++, data.getLastResetTime());
			call.setBoolean(i++, data.isEnableAutoRecycle());
			call.setString(i++, StringUtils.listToString(data.getAutoRecycleList(), ','));
			return call.executeUpdate() > 0;
		} catch (Exception e) {
			logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
		} finally {
			getDb().closeConnection(call, null);
		}
		return false;
	}

	@Override
	protected boolean updateData(UserVehicleDetail data) {
		PreparedStatement call = null;
		String sql = "UPDATE t_u_vehicle_detail SET templateIdList=?,modifyTime=?,skillModifyTotalTimes=?,totalCount=?,totalDeleteCount=?,"
				+ "qualitySixAboveTotalCount=?,totalUpgradeTimes=?,lastResetTime=?,enableAutoRecycle=?,autoRecycleList=? WHERE userId=?";
		try {
			getDb().setConnName(DbNameStrategy.getUserDb());
			call = getDb().getConnection().prepareStatement(sql);
			int i = 1;
			call.setString(i++, StringUtils.listToString(data.getTemplateIdList(), ','));
			call.setLong(i++, data.getModifyTime());
			call.setLong(i++, data.getSkillModifyTotalTimes());
			call.setLong(i++, data.getTotalCount());
			call.setLong(i++, data.getTotalDeleteCount());
			call.setLong(i++, data.getQualitySixAboveTotalCount());
			call.setLong(i++, data.getTotalUpgradeTimes());
			call.setLong(i++, data.getLastResetTime());
			call.setBoolean(i++, data.isEnableAutoRecycle());
			call.setString(i++, StringUtils.listToString(data.getAutoRecycleList(), ','));
			call.setLong(i++, data.getUserId());
			return call.executeUpdate() > 0;
		} catch (Exception e) {
			logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
		} finally {
			getDb().closeConnection(call, null);
		}
		return false;
	}

	@Override
	protected UserVehicleDetail parseData(ResultSet rs) throws SQLException {
		UserVehicleDetail data = new UserVehicleDetail();
		data.setUserId(rs.getLong("userId"));
		data.setModifyTime(rs.getLong("modifyTime"));
		data.setSkillModifyTotalTimes(rs.getLong("skillModifyTotalTimes"));
		data.setTotalDeleteCount(rs.getLong("totalDeleteCount"));
		data.setTemplateIdList(StringUtils.stringToIntegerListNoZero(rs.getString("templateIdList"), ","));
		data.setTotalCount(rs.getLong("totalCount"));
		data.setQualitySixAboveTotalCount(rs.getLong("qualitySixAboveTotalCount"));
		data.setTotalUpgradeTimes(rs.getLong("totalUpgradeTimes"));
		data.setLastResetTime(rs.getLong("lastResetTime"));
		data.setEnableAutoRecycle(rs.getBoolean("enableAutoRecycle"));
		String autoRecycleListStr = rs.getString("autoRecycleList");
		if (autoRecycleListStr != null) {
			data.setAutoRecycleList(StringUtils.stringToIntegerListNoZero(autoRecycleListStr, ","));
		}
		return data;
	}
}
